package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import model.Semester;
import service.impl.CloseConnections;

public class SemesterDAO {
	static Connection currentConnection = null;
	static ResultSet rs = null;

	public static List<Semester> get() {
		PreparedStatement statement = null;
		List<Semester> semesters = new ArrayList<Semester>();

		try {
			// connect to DB
			currentConnection = ConnectionManager.getConnection();
			String sql = "select * from semester";
			statement = currentConnection.prepareStatement(sql);
			rs = statement.executeQuery();
			while (rs.next()) {
				Semester semester = new Semester();
				semester.setId(rs.getLong("id"));
				semester.setText(rs.getString("text"));
				semesters.add(semester);
			}

		} catch (Exception e) {
			System.out.println("Exception in connection!: " + e);
		} finally {
			CloseConnections.closeAll(rs, statement, currentConnection);
		}
		return semesters;
	}
}
